/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.manager;

import com.inspur.edp.lcm.metadata.api.mvnEntity.MavenPackageRefs;
import com.inspur.edp.lcm.metadata.common.FileServiceImp;
import com.inspur.edp.lcm.metadata.common.MavenUtils;
import com.inspur.edp.lcm.metadata.common.Utils;
import com.inspur.edp.lcm.metadata.core.persistence.RepositoryFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;

import java.io.File;
import java.util.List;

@Slf4j
public class PomManager {
    public static final String API = "api";
    public static final String CORE = "core";
    public static final String COMP = "comp";
    public static final String POM = "pom.xml";

    public void createPomWithSourceModel(Model sourceModel, String pomPath, String repoId, String dependencyVersion) {
        final Model model = createModelWithSourceModel(sourceModel, repoId, dependencyVersion);
        writeModel(pomPath, model);
    }

    public void createPomForDependencyCopy(List<MavenPackageRefs> refs, String pomPath) {
        RepositoryFactory.getInstance().getPomRepository().createPomForDependencyCopy(refs, pomPath);
    }

    public void updatePom(String pomPath, List<MavenPackageRefs> refs, String packaging) {
        RepositoryFactory.getInstance().getPomRepository().updatePom(pomPath, refs, packaging);
    }

    public void setRepoUrl(String pomPath, String repoId) {
        final Model model = getModel(pomPath);
        final DistributionManagement distributionManagement = RepositoryFactory.getInstance().getMavenSettingsRepository().getDistributionManagement(repoId, model.getVersion().endsWith(MavenUtils.snapShot));
        model.setDistributionManagement(distributionManagement);
        writeModel(pomPath, model);
    }

    public void writeModel(String pomPath, Model model) {
        RepositoryFactory.getInstance().getPomRepository().writeModel(pomPath, model);
    }

    public Model getModel(String pomPath) {
        Model model = RepositoryFactory.getInstance().getPomRepository().getModel(pomPath);
        model.setGroupId(model.getGroupId() == null ? model.getParent().getGroupId() : model.getGroupId());
        model.setArtifactId(model.getArtifactId() == null ? model.getParent().getArtifactId() : model.getArtifactId());
        model.setVersion(model.getVersion() == null ? model.getParent().getVersion() : model.getVersion());
        return model;
    }

    public String getParentPomPath(String projectPath) {
        return FileServiceImp.combinePath(projectPath, Utils.getJavaProjectPath(), Utils.getCodePath(), POM);
    }

    public void deleteModules(String pomPath, List<String> modules) {
        RepositoryFactory.getInstance().getPomRepository().deleteModules(pomPath, modules);
    }

    public void transferDependencies(String projectPath, String module, String toModule) {
        String toPomPath = FileServiceImp.combinePath(projectPath, Utils.getJavaProjectPath(), Utils.getCodePath(), toModule, POM);
        if (!new File(toPomPath).exists()) {
            log.info("不存在找到pom文件：[{}]", toPomPath);
            return;
        }
        String fromPomPath = FileServiceImp.combinePath(projectPath, Utils.getJavaProjectPath(), Utils.getCodePath(), module, POM);
        if (!new File(fromPomPath).exists()) {
            log.info("不存在到pom文件：[{}]", fromPomPath);
            return;
        }
        RepositoryFactory.getInstance().getPomRepository().transferDependencies(fromPomPath, toPomPath);
    }

    public void deleteDependency(String projectPath, String module, Dependency dependency) {
        String pomPath = FileServiceImp.combinePath(projectPath, Utils.getJavaProjectPath(), Utils.getCodePath(), module, POM);
        RepositoryFactory.getInstance().getPomRepository().deleteDependency(pomPath, dependency);
    }

    private Model createModelWithSourceModel(Model sourceModel, String repoId, String dependencyVersion) {
        final Model modelWithSourceModel = RepositoryFactory.getInstance().getPomRepository().createModelWithSourceModel(sourceModel, repoId, dependencyVersion);
        return modelWithSourceModel;
    }

    public void deleteApiDependency(String projectPath, String module) {
        String parentPomPath = getParentPomPath(projectPath);
        Dependency dependency = RepositoryFactory.getInstance().getPomRepository().getModule(parentPomPath, PomManager.API);
        String pomPath = FileServiceImp.combinePath(projectPath, Utils.getJavaProjectPath(), Utils.getCodePath(), module, POM);
        RepositoryFactory.getInstance().getPomRepository().deleteDependency(pomPath, dependency);
    }
}
